package com.heyqing.medium.day1;

/**
 * ClassName:LongestPalindrome
 * Package:com.heyqing.medium.day1
 * Description:
 *
 * @Date:2024/10/2
 * @Author:Heyqing
 */
public class LongestPalindrome {
    public String longestPalindrome(String s) {
        int maxVal = 0, minStart = 0, maxEnd = 1;
        for (int i = 0; i < s.length(); i++) {
            if (s.length() - 1 - i <= maxVal) return s.substring(minStart, maxEnd + 1);
            int tempi = i, tempj = 0;
            boolean flag = false;
            for (int j = s.length() - 1; j >= tempi; j--) {
                if (s.charAt(tempi) == s.charAt(j)) {
                    if (!flag) {
                        tempj = j;
                    }
                    flag = true;
                    tempi++;
                } else {
                    if (j != s.length() - 1 && tempi != i) j = tempj;
                    tempi = i;
                    flag = false;
                }
            }
            if (flag) {
                if (tempj - i > maxVal) {
                    minStart = i;
                    maxEnd = tempj;
                    maxVal = maxEnd - minStart;
                }
            }
        }
        return s.substring(minStart, maxEnd + 1);
    }

    public static void start() {
        String s = "xaabacxcabaaxcabaax";
        LongestPalindrome lp = new LongestPalindrome();
        String s1 = lp.longestPalindrome(s);
        System.out.println("res:" + s1);
    }
}
